package com.uyaer.engine.role
{
	import com.uyaer.engine.Engine;
	import com.uyaer.engine.core.Model;
	import com.uyaer.engine.display.Scene;
	import com.uyaer.engine.event.EventManager;
	import com.uyaer.engine.event.EventType;
	import com.uyaer.engine.event.MyEvent;

	public class RolesManager
	{
		private static var myinstance:RolesManager = new RolesManager();
		public var roles:Vector.<Model> = new Vector.<Model>();
		public function RolesManager()
		{
			Engine.instance.ticker.addRefresh(tick);
			EventManager.addEvent(EventType.ASECOND,oneSecondHandler);
		}
		
		private function oneSecondHandler(e:MyEvent):void{
			sortZ();
		}
		
		/**
		 *对z-轴进行排序 
		 * 
		 */		
		private function sortZ():void{
			roles.sort(compareZAxis);
			var len:int = roles.length;
			for(var i:int =0 ;i < len ; i ++){
				Engine.instance.scene.personCon.addChildAt(roles[i] , i);
			}
		}
		
		private function compareZAxis(obj1:Model,obj2:Model):int{
			if(obj1.z_axis<obj2.z_axis){
				return -1;
			}else{
				return 1;
			}
		}
		
		public static function get instance():RolesManager{
			return myinstance;
		}
		
		public function addModel(m:Model):void{
			roles.push(m);
			Engine.instance.scene.personCon.addChild(m);
		}
		
		/**
		 * 每一帧的处理函数
		 * 
		 */		
		private function tick():void{
			for each (var m:Model in roles){
				m.tick();
			}
		}
	}
}